/**
 * 
 */
package webreg.model;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;

import webreg.model.exceptions.EmptyDateException;
import webreg.model.exceptions.EmptyPlaceException;
import webreg.model.exceptions.EmptyValueException;

/**
 * Class for personal best marks. Containing place, value, date of the best value 
 * 
 * @author STKNOLL
 */
@XmlAccessorType(XmlAccessType.NONE)
public class BestPerformance implements Cloneable{

	@XmlAttribute (name = "value")
	private String mValue;
	@XmlAttribute (name = "date")
	private String mDate;
	@XmlAttribute (name = "place")
	private String mPlace;	

	/** 
	 * Default contructor for a best performance 
	 */
	public BestPerformance() {
		this.mValue = "";
		this.mDate = "";
		this.mPlace = "";
	}

	/** Contructs a best performance with a value
	 * @param value The value of the best performance
	 * @throws EmptyValueException 
	 */
	public BestPerformance(String value) throws EmptyValueException {
		this();
		setValue(value);
	}

	/** Contructs a best performance with a value and place
	 * @param value The value of the best performance
	 * @param place The place where the best has been achived 
	 * @throws EmptyValueException 
	 * @throws EmptyPlaceException 
	 */
	public BestPerformance(String value, String place) throws EmptyValueException, EmptyPlaceException {
		this();
		setValue(value);
		setPlace(place);
	}

	/** Contructs a best performance with a value, place and date
	 * @param value The value of the best performance
	 * @param place The place where the best has been achived 
	 * @param date The date when the best has been achived
	 * @throws EmptyDateException 
 	 * @throws EmptyValueException 
	 * @throws EmptyPlaceException 
	 */
	public BestPerformance(String value, String place, String date) throws EmptyDateException,EmptyValueException, EmptyPlaceException {
		this();
		setValue(value);
		setPlace(place);
		setDate(date);
	}

	/**
	 * Creates a best performance based on an already existing one
	 * @param toCopy The best performance to copy
	 * @throws EmptyDateException 
	 * @throws EmptyPlaceException 
	 * @throws EmptyValueException 
	 */
	public BestPerformance(BestPerformance toCopy) {
		this( );
		mValue = toCopy.mValue;
		mPlace = toCopy.mPlace;
		mDate = toCopy.mDate;
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	public boolean equals(BestPerformance toCompare) {
		if (this.mValue.equals(toCompare.mValue) == false) return false;
		if (this.mPlace.equals(toCompare.mPlace) == false) return false;
		if (this.mDate.equals(toCompare.mDate ) == false) return false;
		return true;
	}


	
	/** Returns the date when the personal best has been achived
	 * 
	 * @return the data of the best performance
	 */
	public String getDate() {
		return mDate;
	}

	/** Sets the date when the personal best has been achived
	 * 
	 * @param bestDate The date when the best performance was achived
	 * @throws EmptyDateException 
	 */
	public void setDate(String bestDate) throws EmptyDateException {
		if ( (bestDate == null) || (bestDate.trim().equals("")) ) throw new EmptyDateException("BestPerformance in " + getPlace() );
		this.mDate = bestDate;
	}

	/** Returns the place where the personal best has been achived
	 * 
	 * @return the place where the personal best has been achived
	 */
	public String getPlace() {
		return mPlace;
	}

	/** Sets the place where the personal best has been achived.
	 * 
	 * @param mBestPlace the place where the best performance was achived
	 * @throws EmptyPlaceException 
	 */
	public void setPlace(String bestPlace) throws EmptyPlaceException {
		if ( (bestPlace == null) || (bestPlace.trim().equals("")) ) throw new EmptyPlaceException("BestPerformance at " + getDate());
		this.mPlace = bestPlace;
	}

	/** Returns the value of the personal best
	 * 
	 * @return the value of the personal best
	 */
	public String getValue() {
		return mValue;
	}

	/** Sets the value of the best performance
	 * 
	 * @param bestValue The value of the best performance
	 * @throws EmptyValueException 
	 */
	public void setValue(String bestValue) throws EmptyValueException {
		if ( (bestValue == null) || (bestValue.trim().equals("")) ) throw new EmptyValueException("BestPerformance in " + getPlace());
		this.mValue = bestValue;
	}

	@Override
	public Object clone() {
		BestPerformance newBest = new BestPerformance();
		newBest.mDate = mDate;
		newBest.mPlace = mPlace;
		newBest.mValue = mValue;
		return newBest;
	}

	public int compareTo(BestPerformance other) {
		int valueCompare = mValue.compareTo(other.mValue);
		if (valueCompare != 0) return valueCompare;

		int placeCompare = mPlace.compareTo(other.mPlace);
		if (placeCompare != 0) return placeCompare;

		int dateCompare = mDate.compareTo(other.mDate);
		if (dateCompare != 0 ) return dateCompare;

		return 0;
	}

}
